#include<iostream>
#include<pthread.h>
#include<ctime>
#include<unistd.h>
#include"ringqueue.hpp"
#include"task.hpp"


using namespace std;

void* cosumer(void* args)
{
    Ringqueue<task>* rq = static_cast<Ringqueue<task>*>(args);
    while (true)
    {
        task data = rq->pop();

        cout <<"消费了一个数据： " << data.Getresult() <<endl;
    }

    return nullptr;

}

void* productor(void* args)
{
    Ringqueue<task>* rq = static_cast<Ringqueue<task>*>(args);

    while (true)
    {
        int x = rand() % 10;
        int y = rand() % 10;
        task t(x,y,opers[rand()%opers.size()]);
        sleep(1);
        t.run();
        rq->push(t);
        cout << "生产一个数据：" << t.Gettask() <<endl;
        usleep(1);
    }

    return nullptr;

}


int main()
{

    srand(time(nullptr));
    pthread_t ctid,pptid,ptid;

    Ringqueue<task>* rq = new Ringqueue<task>();
    pthread_create(&ctid,nullptr,cosumer,rq);
    pthread_create(&ptid,nullptr,productor,rq);
    pthread_create(&pptid,nullptr,productor,rq);

    pthread_join(ctid,nullptr);
    pthread_join(ptid,nullptr);

    return 0;
}